package com.nine.mq.controller;

import com.nine.mq.config.constanst.MqConstanst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 延迟消息控制器
 */
@Slf4j
@RestController
@RequestMapping("/delay")
public class DelayController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/send/{msg}")
    public String delay(@PathVariable("msg") String msg) {
        Map<String, Object> map = new HashMap<>();
        map.put("msg", msg);
        log.info("DlxConsumer 时间 {}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        rabbitTemplate.convertAndSend(MqConstanst.DELAY_EXCHANGE, MqConstanst.DELAY_ROUTING_KEY, map, message -> {
            // 设置延迟时间，单位毫秒【队列中也有设置时间，哪个小，以哪个为准】
            message.getMessageProperties().setExpiration("5000");
            return message;
        });

        return "延迟消息";
    }


}
